#region Copyright Notice
// 
// 
//  
//  Copyright (c) 2009-2011 Anindya Chatterjee
//  
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//  
//      http://www.apache.org/licenses/LICENSE-2.0
//  
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// 
// 
#endregion
using System;
using AbstractClass.NPerf;
using QuasarDemo.Tests;

namespace QuasarDemo
{
    public class TestFactory
    {
        private readonly NPerfTestRunner _nPerfTestRunner = new NPerfTestRunner();

        public TestFactory(NPerfTestRunner nPerfTestRunner)
        {
            _nPerfTestRunner = nPerfTestRunner;
        }

        public Action CreateTest(string name)
        {
            switch (name)
            {
                case "BSTTest":
                    return GetTestStarter(new BinarySearchTreeTest(), "Starting Binary Search Tree Test");
                case "BTTest":
                    return GetTestStarter(new BinaryTreeTest(), "Starting Binary Tree Test");
                case "TreeTest":
                    return GetTestStarter(new TreeTest(), "Starting Generic Tree Test");
                case "ExtensionTest":
                    return GetTestStarter(new ExtensionTest(), "Starting Extension Methods Test");
                case "AVLTest":
                    return GetTestStarter(new AVLTreeTest(), "Starting AVL Tree Test");
                case "BagTest":
                    return GetTestStarter(new PocketTest(), "Starting Pocket Test");
                case "ConditionsTest":
                    return GetTestStarter(new ConditionsTest(), "Starting Conditions Test");
                case "GraphTest":
                    return GetTestStarter(new GraphTest(), "Starting Graph Test");
                case "CacheTest":
                    return GetTestStarter(new CacheTest(), "Starting Cache Test");
                default:
                    return null;
            }
        }

        private Action GetTestStarter(AbstractTest test, string header)
        {
            var action = new Action(delegate
                                        {
                                            TestFixture fixture = TestLoader.LoadType(test.GetType());
                                            test.PrintHeader(header);
                                            _nPerfTestRunner.TestSuiteEnd +=
                                                delegate(object sender, TestSuiteRunEventArgs e)
                                                    {
                                                        int ignored = e.TestSuite.IgnoredTests.Count;
                                                        int failed = e.TestSuite.FailedResult.Count;
                                                        int total = e.TestSuite.Tests.Count;
                                                        _nPerfTestRunner.ResultWriter.WriteLine(
                                                            "Test Finished:  Successful - {0}; Ignored - {1}; Failed - {2}",
                                                            total - failed - ignored, ignored, failed);
                                                    };
                                            _nPerfTestRunner.RunTestFixture(fixture, false);
                                            _nPerfTestRunner.QueryCompletedTests();
                                        });
            return action;
        }
    }
}